//
//  HRZAddressDataBase.m
//  EMarket
//
//  Created by 石头 on 2018/5/16.
//  Copyright © 2018年 好日子. All rights reserved.
//

#import "HRZAddressDataBase.h"

#import "HRZAddressItem.h"

#import <FMDB.h>

@interface HRZAddressDataBase()<NSCopying, NSMutableCopying>
{
    FMDatabase *_db;
}
@end

static HRZAddressDataBase *_DBCtl = nil;

@implementation HRZAddressDataBase

+ (instancetype)sharedDataBase {
    if (_DBCtl == nil) {
        _DBCtl = [[HRZAddressDataBase alloc] init];
        [_DBCtl initDataBase];
    }
    return _DBCtl;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    if (!_DBCtl) {
        _DBCtl = [super allocWithZone:zone];
    }
    return _DBCtl;
}

- (id)copy {
    return self;
}

- (id)mutableCopy {
    return  self;
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return self;
}

#pragma mark - 初始化
- (void)initDataBase {
    /** document Path */
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    /** file Path */
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"address.sqlite"];
    
    /** 实例化FMDataBase */
    _db = [FMDatabase databaseWithPath:filePath];
    
    /** 初始化table */
    [_db open];
    NSString *personSql = @"CREATE TABLE 'adress' ('id' INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL ,'adress_id' VARCHAR(255),'userName' VARCHAR(255),'userPhone' VARCHAR(255),'chooseAdress' VARCHAR(255),'userAdress' VARCHAR(255),'isDefault' VARCHAR(255))";
    [_db executeUpdate:personSql];
    [_db close];
}

#pragma mark - add
- (void)addNewAdress:(HRZAddressItem *)adress {
    [_db open];
    
    NSNumber *maxID = @(0);
    
    FMResultSet *res = [_db executeQuery:@"SELECT * FROM adress"];
    
    while ([res next]) {
        if ([maxID integerValue] < [[res stringForColumn:@"adress_id"] integerValue]) {
            maxID = @([[res stringForColumn:@"adress_id"] integerValue]);
        }
    }
    
    maxID = @([maxID integerValue] + 1);
    
    [_db executeUpdate:@"INSERT INTO adress(adress_id,userName,userPhone,chooseAdress,userAdress,isDefault)VALUES(?,?,?,?,?,?)",maxID,adress.userName,adress.userPhone,adress.chooseAdress,adress.userAdress,adress.isDefault];
    
    [_db close];
}

- (void)deleteAdress:(HRZAddressItem *)adress {
    [_db open];
    
    [_db executeUpdate:@"DELETE FROM adress WHERE adress_id = ?",adress.ID];
    
    [_db close];
}

- (void)updateAdress:(HRZAddressItem *)adress {
    [_db open];
    
    [_db executeUpdate:@"UPDATE 'adress' SET userName = ?  WHERE adress_id = ? ",adress.userName,adress.ID];
    [_db executeUpdate:@"UPDATE 'adress' SET userPhone = ?  WHERE adress_id = ? ",adress.userPhone,adress.ID];
    [_db executeUpdate:@"UPDATE 'adress' SET chooseAdress = ? WHERE adress_id = ? ",adress.chooseAdress,adress.ID];
    [_db executeUpdate:@"UPDATE 'adress' SET userAdress = ? WHERE adress_id = ? ",adress.userAdress,adress.ID];
    [_db executeUpdate:@"UPDATE 'adress' SET isDefault = ?  WHERE adress_id = ? ",adress.isDefault,adress.ID];
    
    [_db close];
}

- (NSMutableArray *)getAllAdressItem {
    [_db open];
    
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    
    FMResultSet *res = [_db executeQuery:@"SELECT * FROM adress"];
    
    while ([res next]) {
        HRZAddressItem *adressItem = [[HRZAddressItem alloc] init];
        adressItem.ID = @([[res stringForColumn:@"adress_id"] integerValue]);
        adressItem.userName = [res stringForColumn:@"userName"];
        adressItem.userPhone = [res stringForColumn:@"userPhone"];
        adressItem.chooseAdress = [res stringForColumn:@"chooseAdress"];
        adressItem.userAdress = [res stringForColumn:@"userAdress"];
        adressItem.isDefault = [res stringForColumn:@"isDefault"];
        [dataArray addObject:adressItem];
    }
    
    [_db close];
    
    return dataArray;
}

- (HRZAddressItem *)defaultAddress {
    
    for (HRZAddressItem *item in [self getAllAdressItem]) {
        if ([item.isDefault isEqualToString:@"2"]) {
            return item;
        }
    }
    return nil;
}

- (void)setDefaultAddress:(HRZAddressItem *)item{}
@end
